function removeAds_common() {
   try {
       var elements = document.getElementsByTagName("*");
       for (var i = elements.length - 1; i >= 0; i--) {
         var element = elements[i]; 
         if (element.getAttribute("alt") === "今日头条"
//           || element.getAttribute("id") === "comment-area"
          || element.classList.contains("toutiao-loading")
          || element.classList.contains("feed-top-refresh")
          || element.classList.contains("m-header-wrapper")
          || element.classList.contains("float-activate-button")
          || element.classList.contains("activate-bar-container")
          || element.classList.contains("feed-top-refresh")
          || element.classList.contains("download-bar__container")
          || element.classList.contains("commentbar")
          || element.classList.contains("arco-masking")
          || element.classList.contains("arco-show-monitor")) {
           element.parentNode.removeChild(element);
         }
       }
   } catch (error) {
       console.log("removeAds:" + error);
   }
}

function removeAds_aticle() {
  try {
    var head = document.getElementsByClassName("article__header")[0];
    var container = document.getElementsByClassName("article-container")[0];
    var content = "<body>" + head.outerHTML + container.outerHTML + "</body>";
    document.body.outerHTML = content;   

    var imgList = document.getElementsByClassName("pgc-img");
    for(var i=0; i<imgList.length; i++) {
      var image = imgList[i].firstElementChild;
      image.setAttribute("style", "max-width:100%;")
    }
    // var json = {
    //   fileName: "aticle.html",
    //   content: "<html>"+document.head.outerHTML+document.body.outerHTML+"<html/>"
    // };
    // window.JsBridge.callNative("onSaveFile", JSON.stringify(json));
  } catch (error) {
    console.log("removeAds: " + error);
  }
}

function snifferPlayInfo() {
  try {
    var name = document.getElementsByClassName("video-title")[0].innerText;
    var image = document.getElementsByClassName("xgplayer-poster")[0].getAttribute("style").replace("background-image:url(", "").replace("\)","");
    // var video = document.getElementsByTagName("source")[0].getAttribute("src");
    var video = document.getElementsByTagName("source")[1].getAttribute("src");

    var player_data = {
      title: name,
      imageUrl: image,
      url: video,
      link: document.URL
    };
    const result = JSON.stringify(player_data);
//      console.log(result);
    window.JsBridge.callNative("onPlayInfoLoaded", result);
//    if (!tryloadNext()) {
//        setTimeout(tryloadNext(), 5000);
//    }
  } catch (error) {
      console.log("snifferPlayInfo:" + error);
  }
}

function tryloadNext() {
  var elements = document.getElementsByClassName("video-container");
  console.log("推荐列表length:" + elements.length);
  if (elements.length > 0) {
    var element = elements[0];
    var event = new MouseEvent("click", {
      view: window,
      bubbles: true,
      cancelable: true
    });
    element.dispatchEvent(event);
    return true;
  }
  return false;
}

function removeAds() {
  if (document.URL.startsWith("https://m.toutiao.com/?W2atIF=1")) {
    removeAds_common();
    fixTransform();
  }
  if (document.URL.startsWith("https://m.toutiao.com/article/")) {
    removeAds_aticle();
  } else if (document.URL.startsWith("https://m.toutiao.com/video/")) {
    snifferPlayInfo();
    // removeAds_common();
    // remove_xgplayer();
  } else {
    removeAds_common();
  }
}

function remove_xgplayer() {
  var removed = false;
  var list = document.getElementsByClassName("xgplayer-wrapper");
  for(let i = 0; i < list.length; i++) {
    var element = list[i];
    if (element.innerText == "APP内打开") {
      element.parentNode.removeChild(element);
      removed = true;
    }
  }
  if (!removed) {
    setTimeout(remove_xgplayer(), 1000);
  } 
}

function fixTransform() {
  var containerArray = document.getElementsByClassName("pull-container");
  if (containerArray.length > 0) {
    var pullContainer = containerArray[0];
    if (pullContainer.hasAttribute("style")){
      pullContainer.removeAttribute("style");
    }
  }
  var loadding = document.getElementsByClassName("toutiao-loading");
  if (loadding.length > 0) {
    loadding[0].parentNode.removeChild(loadding[0]);
  }
  setTimeout(fixTransform(), 1000);
}